import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Row, Col, Form, Spin} from 'antd';
import routes from './routes';
import Return from '../common/Return';
import {FORM_OPTIONS, FORM_ROW_OPTIONS, DATE_FORMAT} from '../constants';
import {getDetail} from './api';
import {vehicleStatus, vehicleProperty, vehiclePurchasePurpose, vehicleClaimType, powerType, transmissionType} from '../Enum';
import WrappedSelect from '../common/WrappedSelect';
import TextInput from 'Shared/components/TextInput';
import {conventEnumValueToString, formatDateTime, formatAmount} from '../utils';
import LocalizeInputNumber from '../common/LocalizeInputNumber';
import WrappedDatePicker from '../common/WrappedDatePicker';
import {language, injectIntl} from './intl';
const FormItem = Form.Item;
const vehicleClaimTypeList = vehicleClaimType.toList(language);
const powerTypeList = powerType.toList(language);
const transmissionTypeList = transmissionType.toList(language);
export class NewPanel extends React.PureComponent {
    state = {
        isValidate: false,
        hasOperat: false,
        loading: false,
        data: {},
    }
    componentDidMount() {
        if(this.props.id)
            this.getDetailInfo();
    }
    componentWillUnmount() {
        this.isunmount = true;
    }
    getDetailInfo = () => {
        const id = this.props.id;
        this.setState({loading: true});
        getDetail(id)
            .then(res => {
                if(res.ok) 
                    this.setState({
                        loading: false,
                        data: res.data
                    });
                else
                    this.setState({
                        loading: false
                    });
            });
    }
    onChange = (value, name) => {
        const data = Object.assign({}, this.state.data, {
            [name]: value
        });
        this.setState({
            data,
            hasOperat: true
        });
    }
    onSubmit = () => {
        const data = this.state.data;
        this.setState({loading: true});
        this.props.onSubmit(data).then(res => {
            if(res.success)
                this.props.successReturn();
            if(!this.isunmount)
                this.setState({loading: false});
        });
    };
    onClickReturnBtn = () => {
        this.props.onCancel();
    }
    render() {
        const {data} = this.state;
        const {formatMessage} = this.props.intl;
        return (
            <div>
                <Spin spinning={this.state.loading}>
                    <Form className="form-standard">
                        <Card title={formatMessage({
                            id: 'newPanel.card.title',
                            defaultMessage: '售后车辆信息'
                        })}>
                            <Row>
                                {!this.props.isDealerUser &&
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.wholesaleDealerCode',
                                        defaultMessage: '总代编号'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.wholesaleDealerCode}
                                    </FormItem>
                                </Col>
                                }
                                {!this.props.isDealerUser &&
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.wholesaleDealerName',
                                        defaultMessage: '总代名称'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.wholesaleDealerName}
                                    </FormItem>
                                </Col>
                                }
                                {!this.props.isDealerUser &&
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.countryName',
                                        defaultMessage: '国家'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.countryName}
                                    </FormItem>
                                </Col>
                                }
                                {!this.props.isDealerUser &&
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.registrationCity',
                                        defaultMessage: '城市'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.registrationCity}
                                    </FormItem>
                                </Col>
                                }
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.salesDealerCode',
                                        defaultMessage: '售出经销商编号'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.salesDealerCode}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.salesDealerName',
                                        defaultMessage: '售出经销商名称'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.salesDealerName}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.vin',
                                        defaultMessage: 'VIN'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.vin}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.status',
                                        defaultMessage: '状态'
                                    })}{...FORM_OPTIONS.item}>
                                        {conventEnumValueToString(vehicleStatus, data.status)}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.productCategoryCode',
                                        defaultMessage: '车型编号'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.productCategoryCode}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.kdProductCategoryCode',
                                        defaultMessage: 'KD车型编号'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.kdProductCategoryCode}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.annoucementNumber',
                                        defaultMessage: '公告号'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.annoucementNumber}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.productCode',
                                        defaultMessage: '产品编号'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.productCode}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.productName',
                                        defaultMessage: '产品名称'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.productName}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.kdProductCode',
                                        defaultMessage: 'KD产品编号'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.kdProductCode}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.kdProductName',
                                        defaultMessage: 'KD产品名称'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.kdProductName}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.engineModel',
                                        defaultMessage: '发动机型号'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.engineModel}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.transmissionModel',
                                        defaultMessage: '变速箱型号'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.transmissionModel}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.transmission',
                                        defaultMessage: '手自动'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.transmission}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.drive',
                                        defaultMessage: '驱动'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.drive}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.ludder',
                                        defaultMessage: '左右舵'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.ludder}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.engineCylinder',
                                        defaultMessage: '排量'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.engineCylinder}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.color',
                                        defaultMessage: '颜色'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.color}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.rolloutDate',
                                        defaultMessage: '生产日期'
                                    })}{...FORM_OPTIONS.item}>
                                        {formatDateTime(data.rolloutDate, DATE_FORMAT)}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.shipDate',
                                        defaultMessage: '发车日期'
                                    })}{...FORM_OPTIONS.item}>
                                        {formatDateTime(data.shipDate, DATE_FORMAT)}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.saleDate',
                                        defaultMessage: '实销日期'
                                    })}{...FORM_OPTIONS.item}>
                                        {formatDateTime(data.saleDate, DATE_FORMAT)}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.keyCode',
                                        defaultMessage: '钥匙号'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.keyCode}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.certificateDate',
                                        defaultMessage: '合格证日期'
                                    })}{...FORM_OPTIONS.item}>
                                        {formatDateTime(data.certificateDate, DATE_FORMAT)}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.certificateNumber',
                                        defaultMessage: '合格证号'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.certificateNumber}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.salePrice',
                                        defaultMessage: '实销价格'
                                    })}{...FORM_OPTIONS.item}>
                                        {formatAmount(data.salePrice)}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.saleInvoiceNumber',
                                        defaultMessage: '实销发票号'
                                    })}{...FORM_OPTIONS.item}>
                                        {formatAmount(data.saleInvoiceNumber)}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.invoiceDate',
                                        defaultMessage: '开发票日期'
                                    })}{...FORM_OPTIONS.item}>
                                        {formatDateTime(data.invoiceDate, DATE_FORMAT)}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.vehicleDeliveryDate',
                                        defaultMessage: '交车日期'
                                    })}{...FORM_OPTIONS.item}>
                                        {formatDateTime(data.vehicleDeliveryDate, DATE_FORMAT)}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.vehicleProperty',
                                        defaultMessage: '车辆属性'
                                    })}{...FORM_OPTIONS.item}>
                                        {conventEnumValueToString(vehicleProperty, data.vehicleProperty)}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.purchasePurpose',
                                        defaultMessage: '购车用途'
                                    })}{...FORM_OPTIONS.item}>
                                        {conventEnumValueToString(vehiclePurchasePurpose, data.purchasePurpose)}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.manufacturer',
                                        defaultMessage: '生产工厂'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.manufacturer}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.isNewPower',
                                        defaultMessage: '是否新能源'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.isNewPower ? this.props.intl.formatMessage({
                                            id: 'detailPanel.data.yes',
                                            defaultMessage: '是',
                                        }) : formatMessage({
                                            id: 'detailPanel.data.no',
                                            defaultMessage: '否',
                                        })}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.isOtherBrand',
                                        defaultMessage: '是否他牌车'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.isOtherBrand ? this.props.intl.formatMessage({
                                            id: 'detailPanel.data.yes',
                                            defaultMessage: '是',
                                        }) : formatMessage({
                                            id: 'detailPanel.data.no',
                                            defaultMessage: '否',
                                        })}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.transmissionType',
                                            defaultMessage: '变速箱类型'
                                        })}
                                        {...FORM_OPTIONS.item}>
                                        <WrappedSelect
                                            allowClear
                                            name="transmissionType"
                                            value={data.transmissionType}
                                            options={transmissionTypeList}
                                            onChange={this.onChange} />
                                    </FormItem>
                                </Col>
                            </Row>
                            <Row>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.engineCode',
                                        defaultMessage: '发动机号'
                                    })}{...FORM_OPTIONS.item}>
                                        <TextInput
                                            name="engineCode"
                                            value={data.engineCode}
                                            onBlur={this.onChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.transmissionSn',
                                        defaultMessage: '变速箱号'
                                    })}{...FORM_OPTIONS.item}>
                                        <TextInput
                                            name="transmissionSn"
                                            value={data.transmissionSn}
                                            onBlur={this.onChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.licensePlate',
                                        defaultMessage: '车牌号'
                                    })}{...FORM_OPTIONS.item}>
                                        <TextInput
                                            name="licensePlate"
                                            value={data.licensePlate}
                                            onBlur={this.onChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.operation.mileage',
                                        defaultMessage: '行驶里程'
                                    })} {...FORM_OPTIONS.item}>
                                        <LocalizeInputNumber
                                            name="mileage"
                                            value={data.mileage}
                                            onBlur={this.onChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.vehicleClaimType',
                                            defaultMessage: '索赔类别'
                                        })}
                                        {...FORM_OPTIONS.item}>
                                        <WrappedSelect
                                            allowClear
                                            name="vehicleClaimType"
                                            value={data.vehicleClaimType}
                                            options={vehicleClaimTypeList}
                                            onChange={this.onChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.powerType',
                                            defaultMessage: '动力类型'
                                        })}
                                        {...FORM_OPTIONS.item}>
                                        <WrappedSelect
                                            allowClear
                                            name="powerType"
                                            value={data.powerType}
                                            options={powerTypeList}
                                            onChange={this.onChange} />
                                    </FormItem>
                                </Col>
                            </Row>
                            <Row className="row-margin">
                                <Col>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.text.remark',
                                        defaultMessage: '备注'
                                    })} {...FORM_ROW_OPTIONS.item}>
                                        <TextInput
                                            type="textarea"
                                            name="remark"
                                            value={data.remark}
                                            onBlur={this.onChange} />
                                    </FormItem>
                                </Col>
                            </Row>
                        </Card>
                    </Form>
                    <Card>
                        <Row>
                            <Col className="col-align-right">
                                <Button key="audit"
                                    onClick={this.onSubmit}
                                    type="primary"
                                    loading={this.state.loading}>
                                    {formatMessage({
                                        id: 'newPanel.btn.submit',
                                        defaultMessage: '保存'
                                    })}</Button>
                            </Col>
                        </Row>
                    </Card>
                </Spin>
                <div className="page-toolbar">
                    <Return
                        onConfirm={this.onClickReturnBtn}
                        disabled={this.state.loading}
                        showWarning={this.state.hasOperat} />
                </div>
            </div>
        );
    }
}

NewPanel.propTypes = {
    successReturn: PropTypes.func.isRequired,
    onCancel: PropTypes.func.isRequired,
    onSubmit: PropTypes.func.isRequired,
    intl: PropTypes.object,
};

import {connect} from 'react-redux';
import {tableSearch, onSubmit} from './actions';
import {createSelector} from 'reselect';
const getinitEnumData = createSelector(
    state => state.getIn(['page', 'domainData', 'init']),
    init => {
        const initData = init.toJS();
        const brands = initData.brands ? initData.brands.map(item => ({
            text: `(${item.code})${item.name}`,
            value: item.id
        })) : [];
        return {
            brands
        };
    }
);
const mapStateToProps = state => ({
    initEnumData: getinitEnumData(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    onSubmit: data => dispatch(onSubmit(data)),
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(NewPanel));
